﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace BigVisibleCountdown
{
    public class CountdownTimer : INotifyPropertyChanged
    {

        private TimeSpan _time;

        public int Hours
        {
            get { return (int)_time.Hours; }
            set
            {
                UpdateTimeSpan(new TimeSpan(value, _time.Minutes, _time.Minutes));
            }
        }

        public int Minutes
        {
            get { return (int)_time.Minutes; }
            set
            {
                UpdateTimeSpan(new TimeSpan(_time.Hours, value, _time.Minutes));
            }
        }
        
        public int Seconds
        {
            get { return (int)_time.Seconds; }
            set
            {
                UpdateTimeSpan(new TimeSpan(_time.Hours, _time.Minutes, value));
            }
        }

        public CountdownTimer() : this(0, 0, 0)
        {
 
        }

        public CountdownTimer Me
        {
            get { return this; }
        }

        public CountdownTimer(int hours, int minutes, int seconds)
        {
            _time = new TimeSpan(hours, minutes, seconds);
        }

        public void DecrementBy(TimeSpan duration)
        {
            if ((_time - duration) >= TimeSpan.FromSeconds(0))
            {
                UpdateTimeSpan(_time - duration);
            }
            else
            {
                UpdateTimeSpan(TimeSpan.FromSeconds(0));
            }
            
        }

        public void IncrementBy(TimeSpan duration)
        {
            UpdateTimeSpan(_time + duration);
        }

        protected void UpdateTimeSpan(TimeSpan timeSpan)
        {
            _time = timeSpan;
            NotifyPropertyChanged("Hours");
            NotifyPropertyChanged("Minutes");
            NotifyPropertyChanged("Seconds");
            NotifyPropertyChanged("Me");
        }

        public void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
}
